package com.crm.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.crm.easyui.Grid;
import com.crm.po.Customer;
import com.crm.service.CustomerService;
import com.crm.util.ResponseUtils;

public class CustomerAction extends Servlet {

	private static final long serialVersionUID = 1116994082953057010L;
	
	@SuppressWarnings("unchecked")
	public void getCustomerList(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		CustomerService service = new CustomerService();
		Map<String, Object> map = service.getCustomers(request);
		
		Grid grid = new Grid();
		grid.setTotal((Integer)map.get("count"));
		grid.setRows((List<Customer>)map.get("rows"));
		
		ResponseUtils.printJson(response, null, grid, null,null);
	}
	
	public void saveOrUpdateCustomer(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		CustomerService service = new CustomerService();

		boolean result = service.saveOrUpdateCustomer(request);
		
		String value = result ? "ok": "新增或修改数据失败，请检查您的输入数据是否有效。";
		ResponseUtils.printString(response, value);
	}
	
	public void deleteCustomers(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		CustomerService service = new CustomerService();

		boolean result = service.deleteCustomers(request);
		
		String value = result ? "ok": "删除数据失败，请检查您的系统是否出现错误。";
		ResponseUtils.printString(response, value);
	}
	
	public void recoverCustomers(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		CustomerService service = new CustomerService();

		boolean result = service.recoverCustomers(request);
		
		String value = result ? "ok": "恢复数据失败，请检查您的系统是否出现错误。";
		ResponseUtils.printString(response, value);
	}
	
	public void reallyDeleteCustomers(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		CustomerService service = new CustomerService();

		boolean result = service.reallyDeleteCustomers(request);
		
		String value = result ? "ok": "彻底删除数据失败，请检查您的系统是否出现错误。";
		ResponseUtils.printString(response, value);
	}
	
	
	public void exportCustomerExcel(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		CustomerService service = new CustomerService();
		service.exportCustomers(request, response);
	}
	
}
